import { InjectionKey } from 'vue'
import { createStore, Store, useStore as baseUseStore } from 'vuex'

// 用户 id 在 token 中
// er 为用户角色
export interface State {
  token: string,
  ct: string,
  er: string
}

export const key: InjectionKey<Store<State>> = Symbol('store-state')

export const store = createStore<State>({
  state: {
    token: '',
    ct: '0',
    er: ''
  },
  mutations: {
    setToken (state, newToken) {
      state.token = newToken
    },
    resetToken (state) {
      state.token = ''
    },
    setCt (state, newCt) {
      state.ct = newCt
    },
    resetCt (state) {
      state.ct = '0'
    },
    setRole (state, newRole) {
      state.er = newRole
    }
  }
})

export function useStore () {
  return baseUseStore(key)
}
